package com.longshen.dubbo.api.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.longshen.dubbo.api.mapper.AdminTradeMapper;
import com.longshen.dubbo.api.model.AdminTrade;
import com.longshen.dubbo.api.sequence.SequenceService;
import com.longshen.dubbo.api.service.AdminTradeService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class AdminTradeServiceImpl implements AdminTradeService {

	@Autowired
	private AdminTradeMapper mapper;

	@Autowired
	private SequenceService seq;

	@Override
	public AdminTrade add(AdminTrade in) {
		in.setId(seq.getId());
		 mapper.insert(in);
		return in;
	}

	@Override
	public void mod(AdminTrade in) {
		mapper.updateByPrimaryKeySelective(in);

	}

	@Override
	public void delById(String id) {
		mapper.deleteByPrimaryKey(id);

	}

	@Override
	public Page<AdminTrade> queryPage(Integer pagesize, Integer pagenum, String tradeCode, String tradeName) {
		Example example = new Example(AdminTrade.class);
		
		Criteria criteria=example.createCriteria();
		if (StringUtils.isNotBlank(tradeCode)) {
			tradeCode = "%" + tradeCode + "%";
			criteria.andLike("tradeCode", tradeCode);
		}
		if (StringUtils.isNotBlank(tradeName)) {
			tradeName = "%" + tradeName + "%";
			criteria.andLike("tradeName", tradeName);
		}
		example.setOrderByClause("id");
		Page<AdminTrade> page = PageHelper.startPage(pagenum, pagesize)
				.doSelectPage(() -> mapper.selectByExample(example));
		return page;
	}

}
